var diskStatusApp;

function onLoad() {
    require(["css!../console/globalStatus.css"]);
    initStatus();
}
function initStatus(){
    let app = {
        data() {
            return {
                disks: []
            }
        },
        methods: {
            setDiskData(diskdata) {
                let app_disk = new Array();
                for (let d in diskdata) {
                    d = diskdata[d];
                    let per = (d.usable * 100 / d.total).toFixed(1);
                    let disk = {
                        usable: getReadDataSize(d.usable),
                        used: getReadDataSize(d.total - d.free),
                        free: getReadDataSize(d.free),
                        color: getDiskSizeColor(100 - per),
                        total: getReadDataSize(d.total),
                        percent: per,
                        name: d.name
                    };
                    app_disk.push(disk);
                }
                this.disks = app_disk;
            }
        }
    }

    $.getJSON("../status/disk", null, (data) => {
        diskStatusApp = Vue.createApp(app).mount(".disk-status");
        diskStatusApp.$nextTick(function() {
            this.setDiskData(data);
        })
    })

    function getReadDataSize(dataSize) {
        if (dataSize < 1024) return dataSize + "B";
        if (dataSize < 1048576) return (dataSize / 1024).toFixed(1) + "KB";
        if (dataSize < 1073741824) return (dataSize / 1048576).toFixed(1) + "MB";
        if (dataSize < 1.09951162778e+12) return (dataSize / 1073741824).toFixed(1) + "GB";
        return (dataSize / 1.09951162778e+12).toFixed(1) + "TB";
    }

    function getDiskSizeColor(percent) {
        if (percent < 75) return "info";
        else if (percent < 90) return "warning";
        return "danger";
    }
}
